package tree;

/**
 * 给定一个二叉树，判断它是否是高度平衡的二叉树。
 * @author le
 */
public class BalancedBinaryTree
{
    public static void main(String[] args)
    {

    }

    public boolean isBalanced(TreeNode root) {
        return height(root) >= 0;
    }

    public int height(TreeNode root){
        if (root == null){
            return 0;
        }
        int left = height(root.left);
        int right = height(root.right);
        if (left == -1 || right == -1 || Math.abs(left-right)>1){
            return -1;
        }else{
            return Math.max(left,right)+1;
        }
    }
}
